import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QStackedWidget, QPushButton, QHBoxLayout
from face_module import FaceWidget
from gesture_module import GestureWidget
from speech_module import SpeechWidget

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        print("Initializing MainWindow...")
        self.setWindowTitle("Face & Gesture & Speech System")
        self.setGeometry(100, 100, 1200, 800)

        print("Creating widgets...")
        self.stack = QStackedWidget(self)
        self.face_widget = FaceWidget()
        self.gesture_widget = GestureWidget()
        self.speech_widget = SpeechWidget()
        
        self.stack.addWidget(self.face_widget)
        self.stack.addWidget(self.gesture_widget)
        self.stack.addWidget(self.speech_widget)

        # 创建按钮
        btn_face = QPushButton("人脸识别")
        btn_gesture = QPushButton("手势识别")
        btn_speech = QPushButton("语音输入")
        btn_reinit = QPushButton("重新初始化摄像头")
        
        # 连接信号
        btn_face.clicked.connect(self.switch_to_face)
        btn_gesture.clicked.connect(self.switch_to_gesture)
        btn_speech.clicked.connect(self.switch_to_speech)
        btn_reinit.clicked.connect(self.reinit_camera)

        # 创建水平布局放置按钮
        button_layout = QHBoxLayout()
        button_layout.addWidget(btn_face)
        button_layout.addWidget(btn_gesture)
        button_layout.addWidget(btn_speech)
        button_layout.addWidget(btn_reinit)

        # 主布局
        layout = QVBoxLayout()
        layout.addLayout(button_layout)
        layout.addWidget(self.stack)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

        # 初始显示人脸识别界面
        self.current_module = "face"
        print("Window initialized successfully")

    def switch_to_face(self):
        print("Switching to face tracking...")
        self.stop_current_module()
        if hasattr(self.face_widget, 'start_camera'):
            self.face_widget.start_camera()
        self.stack.setCurrentWidget(self.face_widget)
        self.current_module = "face"

    def switch_to_gesture(self):
        print("Switching to gesture recognition...")
        self.stop_current_module()
        if hasattr(self.gesture_widget, 'start_camera'):
            self.gesture_widget.start_camera()
        self.stack.setCurrentWidget(self.gesture_widget)
        self.current_module = "gesture"

    def switch_to_speech(self):
        print("Switching to speech recognition...")
        self.stop_current_module()
        self.stack.setCurrentWidget(self.speech_widget)
        self.current_module = "speech"

    def stop_current_module(self):
        if self.current_module == "face":
            if hasattr(self.face_widget, 'stop_camera'):
                self.face_widget.stop_camera()
        elif self.current_module == "gesture":
            if hasattr(self.gesture_widget, 'stop_camera'):
                self.gesture_widget.stop_camera()
        elif self.current_module == "speech":
            if hasattr(self.speech_widget, 'stop_listening'):
                self.speech_widget.stop_listening()

    def reinit_camera(self):
        print("Reinitializing camera...")
        if self.current_module == "face":
            if hasattr(self.face_widget, 'reinit_camera'):
                self.face_widget.reinit_camera()
        elif self.current_module == "gesture":
            if hasattr(self.gesture_widget, 'reinit_camera'):
                self.gesture_widget.reinit_camera()

    def closeEvent(self, event):
        self.stop_current_module()
        event.accept()

if __name__ == "__main__":
    print("Starting application...")
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    print("Window shown, entering event loop...")
    sys.exit(app.exec_())
